var treeObj = null;

function getParams() {
    return {"appId": appId, "version": appVerson};
}

$(function () {
    initTree();
    $("#versionSelect").change(function () {
        appVerson = $(this).val() / 1;
        treeObj.setting.async.otherParam = getParams();
        treeObj.reAsyncChildNodes(null, "refresh");
    })
})

function initTree() {
    var setting = {
        view: {
            dblClickExpand: false,
            showLine: false,
            showIcon: false,
            addDiyDom: function (treeId, treeNode) {
                var aObj = $("#" + treeNode.tId + "_a");
                if (treeNode.mVersion > watchVersion) {
                    aObj.css("color", "red");
                }
            },
            selectedMulti: false
        },
        async: {
            dataType: "json",
            enable: true,
            type: "get",
            otherParam: getParams(),
            url: "/app/module/listShow",
            dataFilter: function (treeId, parentNode, responseData) {
                return responseData.data;
            }
        },
        data: {
            key: {
                name: "moduleName"
            },
            simpleData: {
                enable: true,
                idKey: "moduleId",
                pIdKey: "pid",
                rootPId: "0"
            }
        },
        callback: {
            onClick: function (event, treeId, treeNode, treeFlag) {
                if (treeNode.moduleType == 1) {//页面
                    loadPage(treeNode.moduleId);
                } else if (treeNode.moduleType == 0) {
                    treeObj.expandNode(treeNode);
                }
                // treeObj.checkNode(treeNode, !treeNode.checked, false);
            }
        }
    };
    var t = $("#moduleTree");
    t = $.fn.zTree.init(t, setting);
    treeObj = t;
}

function loadPage(id) {
    $(".modulePanel").find('.panel-body').load("/app/module/" + id + ".html", function (response, status, xhr) {
    });
}

function loadUpdatePage(id) {
    window.location.href = "/app/module/update/" + id + ".html";
}